<!-- 生产报表 - 工单进度查询 -->
<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :defaults.sync="defaults"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      :search.sync="search"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="workOrderNoSearch" slot-scope="{ row, size }">
        <el-input placeholder="请输入工单号" v-model="search.workOrderNo" clearable @change="handleEnterWorkOrderNo">
          <el-button slot="append" icon="el-icon-search" @click="handleOpenWorkOrderModel"></el-button
        ></el-input>
      </template>
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true" :loading="exportBtnLoading">导 出</el-button>
      </template>
      <template slot="menu" slot-scope="{ row }">
        <el-button type="text" size="small" icon="el-icon-view" @click="beforeOpen(row, 'view')">详情</el-button>
      </template>
      <!-- <template slot="infoForm">
        <avue-crud ref="infoCrud" :option="infoOption" :data="infoData" @row-del="rowDel" v-model="infoForm" @selection-change="infoSelectionChange"> </avue-crud>
      </template> -->
      <!-- <template #workOrderNo="{ row }">
        <span style="color: #66b1ff; cursor: pointer" @click="goPath(row.workOrderNo)">{{ row.workOrderNo }}</span>
      </template> -->
    </avue-crud>
    <el-dialog
      custom-class="woProgressQueryDialog"
      :title="'详情'"
      append-to-body
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      top="5vh"
      width="80%"
      @close="closeDialog"
    >
      <div class="sail-header-dialog-content">
        <div class="groups">
          <div class="titleIcon">
            <i class="el-icon-vertical-bar avue-group__icon"></i>
            <h4>基本信息</h4>
          </div>
        </div>
        <avue-form ref="steelProperties" v-if="showDialog" v-model="forminfo" :option="dialogOption">
          <!-- <template slot="tensionRequire">
            <div class="range-input" style="display: flex; flex-direction: row">
              <el-input-number placeholder="请输入" v-model="form.tensionMin" controls-position="right" :min="1" :max="1000000"></el-input-number>
              <span> - </span>
              <el-input-number placeholder="请输入" v-model="form.tensionMax" controls-position="right" :min="1" :max="1000000"></el-input-number>
            </div>
          </template> -->
        </avue-form>
        <div class="groups">
          <div class="titleIcon">
            <i class="el-icon-vertical-bar avue-group__icon"></i>
            <h4>过站信息</h4>
          </div>
        </div>
        <avue-crud ref="infoCrud" :option="infoOption" :data="infoData" @row-del="rowDel" v-model="infoForm" @selection-change="infoSelectionChange"> </avue-crud>
        <div class="groups">
          <div class="titleIcon">
            <i class="el-icon-vertical-bar avue-group__icon"></i>
            <h4>产品序列号信息</h4>
          </div>
        </div>
        <avue-crud
          ref="snCrud"
          :table-loading="snLoading"
          @current-change="snCurrentChange"
          @size-change="snSizeChange"
          :page.sync="snPage"
          :option="snOption"
          :data="snData"
          v-model="snForm"
        >
        </avue-crud>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="showDialog = false"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
      </span>
    </el-dialog>
    <export-comp ref="exportComp" page-api-url="mes-abacus/production-report/work-order-progress-page" :option="option" :page="page" :query="query" file-name="生产进度查询">
    </export-comp>

    <global-modal-template :visibile.sync="visibile" :option="tableOption" @getSelectedRows="getSelectedRows" :key="timer" @newKey="newKey"> </global-modal-template>
  </basic-container>
</template>

<script>
import { getList, getDetail, getRouteDetail, getProductDetail } from '@/api/product/workOrderProgressQuery';
import { mapGetters } from 'vuex';
import exportComp from '@/components/export-comp/index';
import userTableConfig from '@/components/user-table-config/index.vue';
import GlobalModalTemplate from '@/components/common-modal-template/global-modal-template.vue';
export default {
  components: {
    // userTableConfig,
    exportComp,
    GlobalModalTemplate
  },
  mixins: [],
  data() {
    return {
      pageCode: 'sectionDefinition',
      visibile: false,
      search: {},
      form: {},
      data: [],
      defaults: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      modalName: '', //打开公用模态框名称
      tableOption: {}, //公用模态框配置信息
      timer: new Date().getTime(),
      selectionList: [],
      option: {
        height: 'calc(100vh - 440px)',
        tip: false,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 4,
        labelWidth: 150,
        dialogCustomClass: 'custom-class',
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        viewBtn: false,
        viewBtnText: '详情',
        viewTitle: '详情',
        editBtn: false,
        delBtn: false,
        refreshBtn: true,
        excelBtn: false,
        selection: true,
        dialogDrag: false, // 开启拖拽功能
        dialogClickModal: false,
        columnBtn: false,
        showSummary: false,
        searchLabelWidth: 100,
        menuWidth: 100,
        column: [
          {
            label: '工单号',
            prop: 'workOrderNo',
            search: true,
            searchOrder: 1,
            viewDisplay: false
          },
          {
            label: '工单类型',
            prop: 'workOrderType',
            type: 'select',
            clearable: false,
            searchValue: '1',
            dicUrl: '/api/blade-system/dict/dictionary?code=order_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            searchOrder: 7,
            search: true
          },
          {
            label: '工单状态',
            prop: 'workOrderStatus',
            type: 'select',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            dicUrl: '/api/blade-system/dict/dictionary?code=mfg_work_order',
            searchOrder: 8,
            search: true
          },
          {
            label: '生产订单',
            prop: 'productionOrderNo',
            searchOrder: 2,
            search: true
          },
          {
            label: '客户',
            prop: 'partnerName',
            search: true,
            searchOrder: 3,
            viewDisplay: false
          },
          {
            label: '产品编码',
            prop: 'partNo',
            search: true,
            searchOrder: 4,
            viewDisplay: false
          },
          {
            label: '产品型号',
            prop: 'partModel',
            search: true,
            searchOrder: 5,
            viewDisplay: false
          },
          {
            label: '产品名称',
            prop: 'partName',
            search: true,
            searchOrder: 6,
            viewDisplay: false
          },

          {
            label: '工单计划数量',
            prop: 'planQuantity',
            viewDisplay: false
          },
          {
            label: '工单完工数量',
            prop: 'completedQuantity',
            viewDisplay: false
          },
          {
            label: '机型',
            prop: 'workProductModel',
            search: true,
            searchOrder: 9,
            viewDisplay: false
          },
          {
            label: '批次',
            prop: 'workLotNo',
            search: true,
            searchOrder: 10,
            viewDisplay: false
          }
        ]
      },
      forminfo: {},
      showDialog: false,
      dialogOption: {
        emptyBtn: false,
        submitBtn: false,
        detail: true,
        labelWidth: 160,
        column: [
          {
            label: '工单号',
            prop: 'workOrderNo',
            span: 8
          },
          {
            label: '工单类型',
            prop: 'workOrderType',
            type: 'select',
            span: 8,
            dicUrl: '/api/blade-system/dict/dictionary?code=order_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '生产订单',
            span: 8,
            prop: 'productionOrderNo'
          },
          {
            label: '销售订单',
            prop: 'salesOrderNo',
            span: 8
          },
          {
            label: '客户名称',
            prop: 'partnerName',
            span: 8
          },
          {
            label: '工单状态',
            prop: 'workOrderStatus',
            type: 'select',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            span: 8,
            dicUrl: '/api/blade-system/dict/dictionary?code=mfg_work_order'
          },
          {
            label: '产品编码',
            prop: 'partNo',
            span: 8
          },
          {
            label: '产品型号',
            prop: 'partModel',
            span: 8
          },
          {
            label: '产品名称',
            prop: 'partName',
            span: 8
          },
          {
            label: 'BOM版本',
            prop: 'bomVersion',
            span: 8
          },
          {
            label: '所属机型',
            prop: 'workProductModel',
            span: 8
          },
          {
            label: '批次',
            prop: 'workLotNo',
            span: 8
          },
          {
            label: '工单计划数量',
            prop: 'planQuantity',
            span: 8
          },
          {
            label: '追加数量',
            prop: 'additionalQuantity',
            span: 8
          },
          {
            label: '制定人',
            prop: 'creator',
            span: 8
          },

          {
            label: '计划开始时间',
            prop: 'planStartDate',
            span: 8
          },
          {
            label: '计划结束时间',
            prop: 'planEndDate',
            span: 8
          },

          {
            label: '最近同步时间',
            prop: 'lastSyncTime',
            span: 8
          },
          {
            label: '备注',
            prop: 'remark',
            type: 'textarea',
            maxRows: 2,
            minRows: 2,
            maxlength: 300,
            overHidden: true,
            showWordLimit: true,
            span: 24
          },
          {
            label: '扩展属性',
            prop: 'extFields',
            type: 'textarea',
            labelTip: '其他系统中工单的个性化参数化信息（MES中并未维护），以json格式同步至此处，可用于工单列表显示等。',
            span: 24,
            maxRows: 2,
            minRows: 2,
            maxlength: 300,
            showWordLimit: true
          }
        ]
      },
      infoForm: {},
      infoOption: {
        selection: false,
        editBtn: false,
        height: 270,
        border: true,
        menuWidth: 100,
        addBtn: false,
        delBtn: true,
        menu: false,
        columnBtn: false,
        refreshBtn: false,
        tip: false,
        column: [
          {
            label: '工序名称',
            prop: 'routeName'
          },
          {
            label: '工序类型',
            prop: 'routeType',
            type: 'select',
            dicData: [
              {
                label: '过站',
                value: 'OVERSTATION'
              },
              {
                label: '组装',
                value: 'ASSEMBLY'
              },
              {
                label: '测试',
                value: 'TEST'
              },
              {
                label: '包装',
                value: 'PACKING'
              },
              {
                label: '老化',
                value: 'AGING'
              },
              {
                label: '报工',
                value: 'TIMESHEET'
              },
              {
                label: '上料',
                value: 'FEEDING'
              }
            ]
          },
          {
            label: '已过站数量',
            prop: 'passQuantity'
          },
          {
            label: '未过站数量',
            prop: 'waitingQuantity'
          },
          {
            label: '不良数',
            prop: 'ngQuantity'
          }
        ]
      },
      infoData: [],
      snForm: {},
      snPage: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      snOption: {
        selection: false,
        editBtn: false,
        height: 270,
        border: true,
        menuWidth: 100,
        addBtn: false,
        delBtn: true,
        menu: false,
        columnBtn: false,
        refreshBtn: false,
        tip: false,
        column: [
          {
            label: '序列号',
            prop: 'productSn'
          },
          {
            label: '当前工序',
            prop: 'currRouteName'
          }
        ]
      },
      snLoading: false,
      snData: [],
      rowData: {},
      exportBtnLoading: false
    };
  },

  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.pallet_add, false),
        viewBtn: this.vaildData(this.permission.pallet_view, false),
        delBtn: this.vaildData(this.permission.pallet_delete, false),
        editBtn: this.vaildData(this.permission.pallet_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    closeDialog() {
      this.leftSelectionList = [];
      this.rightSelectionList = [];
      this.showDialog = false;
    },
    submit() {
      let params = {
        sectionId: this.form.id,
        sectionName: this.form.name,
        sectionCode: this.form.code
      };
      let submitData = this.rightSelectionList.length > 0 ? this.rightSelectionList : this.rightData;

      let data = submitData.map((item) => {
        return {
          lineId: item.id,
          lineName: item.name,
          lineCode: item.code
        };
      });
      bindingLine(data, params).then((res) => {
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
        this.leftSelectionList = [];
        this.rightSelectionList = [];
        this.showDialog = false;
      });
    },
    search() {
      this.getSelectLineRequest(this.leftForm);
    },
    handleSave() {
      add(waitSubmitData).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: 'success',
          message: '保存成功!'
        });
      });
    },
    handleEdit(row) {
      if (this.isSaveDisabled) {
        this.$message({
          type: 'warning',
          message: '请点击保存按钮!'
        });
        return;
      }
      this.data = this._cloneDeep(data);
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return deleteBinding({
            sectionId: this.form.id,
            lineId: row.id
          });
        })
        .then(() => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    beforeClose(done) {
      done();
    },
    beforeOpen(row, type) {
      this.forminfo = {};
      this.infoData = [];
      this.showDialog = true;
      getDetail(row.workOrderId).then((res) => {
        this.forminfo = this._get(res, 'data.data', {});
      });
      getRouteDetail(row.workOrderId).then((res) => {
        this.infoData = this._get(res, 'data.data', {});
      });
      this.rowData.workOrderId = row.workOrderId;
      this.rowData.workOrderType = row.workOrderType;
      this.snPage.currentPage = 1;
      this.handleGetProductData();
      done();
    },
    handleGetProductData() {
      this.snLoading = true;
      getProductDetail(this.snPage.currentPage, this.snPage.pageSize, this.rowData.workOrderId, this.rowData.workOrderType)
        .then((res) => {
          let data = res.data.data;
          this.snData = data.records;
          this.snPage.total = data.total;
        })
        .finally(() => {
          this.snLoading = false;
        });
    },
    searchReset() {
      this.search.workOrderType = '1';
      this.query = {
        workOrderType: 1
      };
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionLeftChange(list) {
      this.leftSelectionList = list;
    },
    selectionRightChange(list) {
      this.rightSelectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      if (this.query.createTime) {
        this.query.startTime = this.$moment(this.query.createTime[0]).format('YYYY-MM-DD HH:mm:ss');
        this.query.endTime = this.$moment(this.query.createTime[1]).format('YYYY-MM-DD HH:mm:ss');
        delete this.query.createTime;
      }
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    /**
     * 重新加载公用表格弹窗组件
     */
    newKey() {
      this.timer = new Date().getTime();
    },
    goPath(workOrderNo) {
      this.$router.push({
        path: '/report/forward-trace-back-to',
        query: { workOrderNo: workOrderNo }
      });
    },
    snCurrentChange(currentPage) {
      this.snPage.currentPage = currentPage;
      this.handleGetProductData();
    },
    snSizeChange(pageSize) {
      this.snPage.pageSize = pageSize;
      this.handleGetProductData();
    },
    handleEnterWorkOrderNo(value) {
      Object.keys(this.search).map((item) => {
        if (!item.startsWith('$') && this.search[item]) {
          this.query[item] = this.search[item];
        }
      });
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },
    handleOpenWorkOrderModel() {
      this.modalName = 'workOrderModal';
      this.visibile = true;
      this.tableOption = {
        modalTitle: '选择工单信息（双击进行选择）',
        url: '/api/mes-product/work-order/page',
        isPage: true,
        rowSelectionType: 'single',
        menu: false,
        editBtn: false,
        menuWidth: 100,
        searchLabelWidth: 90,
        templateName: 'WORKORDER'
      };
    },
    getSelectedRows(rowData) {
      switch (this.modalName) {
        case 'workOrderModal':
          this.$set(this.search, 'workOrderNo', this._get(rowData, 'orderNo', ''));
          break;
        default:
          break;
      }
    }
  }
};
</script>

<style lang="scss">
.btn {
  padding: 9px 15px;
  font-size: 10px;
  height: 35px;
  background-color: #409eff !important;
  color: #cccccc !important;
}

.is-disabled {
  background-color: #fff !important;
  color: #cccccc !important;
}

.title {
  color: #000;
}

.title:hover {
  color: #000;
}

.el-transfer-button {
  display: flex;
  flex-direction: row;
  height: 100%;
  margin: 40px 12px 0;
  align-items: center;
}

.search-container {
  display: flex;
  justify-content: space-between;
}

.search-item {
  display: flex;
  flex-direction: row;
  align-items: center;
}
.el-color-picker__trigger {
  height: 35px;
  width: 35px;
}
.groups {
  margin: 0 0 10px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #dedede;
  .titleIcon {
    display: flex;
    align-items: center;
  }
}

.woProgressQueryDialog {
  .el-dialog__body {
    padding: 20px;
  }
  .avue-crud__menu {
    display: none;
  }
}
</style>
